package com.fast.fast.common.base.vo;

import com.alibaba.fastjson.annotation.JSONField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 接口通用返回对象
 *
 * @author lyf
 * @date 2022/01/01 00:00 周六
 **/
@Accessors(chain = true)
@NoArgsConstructor
@Data
@ApiModel("接口通用返回对象")
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @JSONField(ordinal = 1)
    @ApiModelProperty(value = "返回的成功标识")
    private boolean success = true;

    @JSONField(ordinal = 2)
    @ApiModelProperty(value = "返回的消息")
    private String msg = "操作成功";

    @JSONField(ordinal = 3)
    @ApiModelProperty(value = "返回的状态码")
    private Integer code = 200;

    @JSONField(ordinal = 4)
    @ApiModelProperty(value = "返回的数据对象")
    private T data = null;

    @JSONField(ordinal = 5)
    @ApiModelProperty(value = "返回的时间")
    private String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

    /****************************ok*****************************/

    private Result(boolean success, Integer code) {
        this.setSuccess(success);
        this.setMsg(msg);
        this.setCode(code);
    }

    private Result(boolean success, Integer code, T data) {
        this.setSuccess(success);
        this.setMsg(msg);
        this.setCode(code);
        this.setData(data);
    }

    public static Result<?> ok() {
        return new Result<>(true, 200);
    }

    public static <T> Result<T> ok(T data) {
        return new Result<>(true, 200, data);
    }

    /****************************error****************************/

    private Result(boolean success, String msg, Integer code) {
        this.setSuccess(success);
        this.setMsg(msg);
        this.setCode(code);
    }

    public static <T> Result<T> error() {
        return new Result<>(false, "操作失败", 500);
    }

    public static <T> Result<T> error(String msg) {
        return new Result<>(false, msg, 500);
    }

    public static <T> Result<T> error(String msg, Integer code) {
        return new Result<>(false, msg, code);
    }

}
